package com.rockcent.mall.lib.repository;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rockcent.common.common.PageResult;
import com.rockcent.common.common.SQLListDoing;
import com.rockcent.common.common.SQLPageDoing;
import com.rockcent.common.domain.enumclass.Whether;
import com.rockcent.common.utils.RDateUtils;
import com.rockcent.mall.lib.domain.MallOrder;
import com.rockcent.mall.lib.domain.MerchantOrder;
import com.rockcent.mall.lib.domain.enumclass.MallOrderStatus;
import com.rockcent.mall.lib.domain.enumclass.OrderStatus;
import com.rockcent.mall.lib.domain.enumclass.OrderType;
import com.rockcent.mall.lib.dto.GiftVerifyDto;
import com.rockcent.mall.lib.dto.GiftVerifyItemDto;
import com.rockcent.mall.lib.dto.MallOrderDto;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created by zhangjh on 2016/10/31.
 */
@Repository
public class MallOrderRepositoryImpl implements MallOrderRepositoryCustom {
    private static final Logger log = LoggerFactory.getLogger(MallOrderRepositoryImpl.class);

    @Autowired
    private NamedParameterJdbcTemplate jdbcTemplate;

    @Autowired
    private ObjectMapper objectMapper;


    @Override
    public PageResult<MallOrderDto> findByCustomerIdAndStatusNotIn(Long customerId, List<String> orderStatusList, Boolean isGift, PageResult pageResult) {
        SQLPageDoing<MallOrderDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM mall_order mo WHERE mo.customer_id = :customerId  ");
            if (orderStatusList != null && orderStatusList.size() > 0) {
                sql.append(" AND mo.status NOT IN(:status) ");
            }
            if (isGift != null && isGift) {
                sql.append(" AND mo.gift_id IS NOT NULL ");
            } else {
                sql.append(" AND mo.gift_id IS NULL ");
            }
            sql.append(" ORDER BY mo.date_created desc ");
            fields.append("mo.*");
            params.put("customerId", customerId);
            params.put("status", orderStatusList);
        };
        return doing.go(pageResult, MallOrderDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public PageResult<MallOrderDto> findByCustomerIdAndStatusInPage(Long customerId, List<String> orderStatusList, PageResult pageResult) {
        SQLPageDoing<MallOrderDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM mall_order mo WHERE mo.customer_id = :customerId ");
            if (orderStatusList != null && orderStatusList.size() > 0) {
                sql.append(" AND mo.status IN(:status) ");
            }
            sql.append(" order by mo.date_created desc ");
            fields.append("mo.*");
            params.put("customerId", customerId);
            params.put("status", orderStatusList);
        };
        return doing.go(pageResult, MallOrderDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<MallOrder> queryAllExpiredOrder() {

        SQLListDoing doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append(" select * from   mall_order ");
            sql.append(" where HOUR(timediff(now(),date_created))>=24 ");
            sql.append(" and status ='WAIT_PAYING'");
        };
        return doing.go(MallOrder.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public PageResult<MallOrderDto> findMallBalanceAccount(String beginDate, String endDate, Long mallId, String orderNum, PageResult pageResult) {
        SQLPageDoing<MallOrderDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM mall_order mo WHERE 1=1 ");
            if (StringUtils.isNotBlank(orderNum)) {
                sql.append(" AND mo.order_num = :orderNum ");
                params.put("orderNum", orderNum);
            }
            if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mo.date_created < :endDate AND mo.date_created > :beginDate");
                params.put("beginDate", RDateUtils.beforeDateStr(beginDate, 0));
                params.put("endDate", RDateUtils.afterDateStr(endDate, 1));
            }
            sql.append(" AND mo.mall_id = :mallId ");
            sql.append(" ORDER BY mo.date_created desc ");
            fields.append("mo.*");
            params.put("mallId", mallId);
        };
        return doing.go(pageResult, MallOrderDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public List<MallOrderDto> mallBalanceAccountExport(String beginDate, String endDate, Long mallId, String orderNum) {
        SQLListDoing<MallOrderDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM mall_order mo WHERE 1=1 ");
            if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate)) {
                sql.append(" AND mo.date_created < :endDate AND mo.date_created > :beginDate");
                params.put("beginDate", RDateUtils.beforeDateStr(beginDate, 0));
                params.put("endDate", RDateUtils.afterDateStr(endDate, 1));
            }
            if (StringUtils.isNotBlank(orderNum)) {
                sql.append(" AND mo.order_num = :orderNum ");
                params.put("orderNum", orderNum);
            }
            sql.append(" AND mo.mall_id = :mallId ");
            sql.append(" ORDER BY mo.date_created desc ");
            fields.append("mo.*, mo.date_created createDate");
            params.put("mallId", mallId);
        };
        return doing.go(MallOrderDto.class, objectMapper, jdbcTemplate, log);
    }

    @Override
    public PageResult<MallOrderDto> findByCustomerIdAndStatusIn(Long customerId, List<String> orderStatusList, Boolean isGift, PageResult pageResult) {
        SQLPageDoing<MallOrderDto> doing = (sql, fields, params, typeReferenceMap) -> {
            sql.append("SELECT %s FROM mall_order mo WHERE mo.customer_id = :customerId  ");
            if (orderStatusList != null && orderStatusList.size() > 0) {
                sql.append(" AND mo.status  IN(:status) ");
            }
            if (isGift != null && isGift) {
                sql.append(" AND mo.gift_id IS NOT NULL ");
            } else {
                sql.append(" AND mo.gift_id IS NULL ");
            }
            sql.append(" ORDER BY mo.date_created desc ");
            fields.append("mo.*");
            params.put("customerId", customerId);
            params.put("status", orderStatusList);
        };
        return doing.go(pageResult, MallOrderDto.class, objectMapper, jdbcTemplate, log);
    }


    @Override
    public List<GiftVerifyItemDto> findProductItemsByVerifyPersonId(GiftVerifyDto verifyDto) {
        SQLListDoing<GiftVerifyItemDto> doing = (sql, fields, params, typeReferenceMap) -> {


        };
        return doing.go(GiftVerifyItemDto.class, objectMapper, jdbcTemplate, log);
    }
}
